package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.config.ItemFrignConfig;
import com.hmall.common.config.ItemFrignUser;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.mapper.OrderNumIdMapper;
import com.hmall.order.pojo.*;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utils.ThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
@Transactional
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ItemFrignConfig frignConfig;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ItemFrignUser frignUser;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;
    @Autowired
    private OrderNumIdMapper orderNumIdMapper;
    @Override
    public Long getOrderById(OrderDto orderDto) {
        //      h获取用户id
        Long tl = ThreadLocalUtils.getTl();
//      获取购买数量
        Integer num = orderDto.getNum();
//      付款方式
        Integer paymentType = orderDto.getPaymentType();
//      地址id
        Long addressId = orderDto.getAddressId();
//      商品id
        Long itemId = orderDto.getItemId();
//        根据商品id查询商品信息
        Item item = frignConfig.getByIdController(itemId);
//        数据库中的库存
        Integer status = item.getStatus();

        Long price = item.getPrice();
//        商品总价
        long l = price * num;
//        订单号
        long id = IdWorker.getId();
        //        查出来根据orderid保存
        OrderNum orderNum = new OrderNum();
        orderNum.setNum(status);
        orderNum.setOrderId(id);
        orderNumIdMapper.insert(orderNum);

        Order order = new Order();
        order.setId(id);
        order.setTotalFee(l);
        order.setPaymentType(paymentType);
        order.setUserId(tl);
        order.setStatus(1);
        order.setCreateTime(new Date());
//     订单添加尽数据库
        orderMapper.insert(order);
//        查询订单编号
        List<Order> orders = orderMapper.selectList(new LambdaQueryWrapper<Order>().eq(Order::getUserId, tl));

//       封装商品信息到OrderDetail
        OrderDetail orderDetail = new OrderDetail();
        for (Order order1 : orders) {
            orderDetail.setOrderId(order1.getId());
            orderDetail.setNum(num);
            orderDetail.setItemId(item.getId());
            orderDetail.setName(item.getName());
            orderDetail.setPrice(item.getPrice());
            orderDetail.setSpec(item.getSpec());
            orderDetail.setImage(item.getImage());
            orderDetail.setCreateTime(new Date());
            orderDetail.setUpdateTime(new Date());
            orderDetailMapper.insert(orderDetail);
        }
//        生成物流订单号10位

//        根据id查询用户地址
        List<Address> addresses = frignUser.findAddressByUserId(tl);
        OrderLogistics orderLogistics = new OrderLogistics();
        for (Address address : addresses) {
            BeanUtils.copyProperties(address,orderLogistics);
            orderLogistics.setOrderId(order.getId());
            orderLogistics.setMobile(address.getMobile());
            orderLogisticsMapper.insert(orderLogistics);
            break;
        }
//        减库存
        frignConfig.updateByItemId(itemId,num);
        return order.getId();
    }
}
